package com.biz.primus.ms.member.service;

import com.biz.primus.base.exception.BizBusinessException;
import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.model.member.vo.MemberAuthVo;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.member.dao.redis.MemberAuthRedisDao;
import com.biz.primus.ms.member.dao.ro.MemberAuthRo;
import com.google.common.base.Function;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@Service
public class MemberAuthTokenService extends AbstractBaseService
{

    private final MemberAuthRedisDao memberAuthRedisDao;

    @Autowired
    public MemberAuthTokenService(MemberAuthRedisDao memberAuthRedisDao)
    {
        this.memberAuthRedisDao = memberAuthRedisDao;
    }

    /**
     * 生成新的token并保存
     * @param memberId
     * @return
     */
    public String generateNewToken(Long memberId)
    {
        //生成token
        String token =  UUID.randomUUID().toString().replaceAll("-", "");
        //调用soa服务保存token
        MemberAuthVo memberAuthVo = new MemberAuthVo();
        memberAuthVo.setAuthToken(token);
        memberAuthVo.setId(memberId);

        memberAuthRedisDao.save(new Function<MemberAuthVo, MemberAuthRo>()
        {
            @Override
            public MemberAuthRo apply(MemberAuthVo vo)
            {
                MemberAuthRo ro = new MemberAuthRo();
                BeanUtils.copyProperties(vo, ro);
                ro.setUpdateTimestamp(DateUtil.now());
                return ro;
            }
        }.apply(memberAuthVo));
        return token;

    }

    /**
     * 授权,不通过抛出code 1919或1909异常 客户端将做强退处理
     * @param token
     * @param memberId
     */
    public void authenticate(String token, Long memberId)
    {
        if(token == null)
        {
            throw new BizBusinessException("Please login first");
        }
        MemberAuthVo vo = findById(memberId);
        if(vo == null)
        {
            throw new BizBusinessException("Please login first");
        }
        //在进行具体验证

    }

    /**
     * 刷新token(使用旧token鉴权并创建新token)
     * @param oldToken
     * @param memberId
     * @return
     */
    public String refreshToken(String oldToken, Long memberId)
    {
        authenticate(oldToken, memberId);
        return generateNewToken(memberId);
    }

    /**
     * 根据用户id查询登录token
     * @param memberId
     * @return
     */
    public String findTokenByMemberId(Long memberId){
        MemberAuthVo vo = findById(memberId);
        if(vo == null)
        {
            throw new BizBusinessException("Please login first");
        }
        return vo.getAuthToken();
    }

    public MemberAuthVo findById(Long id) {
        if(id == null) {
            return null;
        }
        return trans(memberAuthRedisDao.findOne(id));
    }

    private MemberAuthVo trans(MemberAuthRo memberAuthRo) {
        if(memberAuthRo == null) {
            return null;
        }
        MemberAuthVo result = new MemberAuthVo();
        BeanUtils.copyProperties(memberAuthRo, result);
        return result;
    }
}
